const app=getApp();
var fakedata=require("../../static/js/fakedata");
Page({
/* eslint-enable */
    data: {
        logoUrl: '../../static/images/miniapp-logo.png', // logo地址
        title: '断舍离', // 小程序名
        btnColor: '#3388FF', // 按钮底色
        descriptions: ['登录帐号可以享受以下功能和权益'], // 推广信息
        features: [{
            name: '商品交易',
            image: '../../static/images/address.png'
        },{
            name: '论坛交流',
            image: '../../static/images/subscription.png'
        },
        {
            name: '我的收藏',
            image: '../../static/images/favorite.png'
        }],
        phone:'',
        password:'',
        cansendcode:true
    },
    inputpass(e){
        this.setData({
            password:e.detail.value
        });
    },
    inputphone(e){
        this.setData({
            phone:e.detail.value
        });
    },
    login:function(){
        if(this.data.phone=='' || this.data.password==''){
            swan.showModal({
                title: '请填写完整的内容'
            });
        }else{
            // 这里的登录接口需要改变，参数使用手机号和密码，方法为post
            swan.request({
                url: fakedata.urls.login,
                header: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Developer-Server-Domain': fakedata.host
                },
                method: 'POST',
                dataType: 'JSON',
                responseType: 'text',
                data: {
                    phone:this.data.phone,
                    code:this.data.password
                },
                success: res => {
                    console.log(res);
                    if(res.statusCode==200){
                        let data=JSON.parse(res.data);
                        if(data.msg=='register'){
                            swan.navigateTo({
                                url:'/pages/register/register?phone='+this.data.phone
                            });
                        }else if(data.msg=='login'){
                            let userinforeturn=data.data;
                            app.globalData.userinfo={
                                nickname:userinforeturn.nickname,
                                id:userinforeturn.id,
                                phone:userinforeturn.phone,
                                wechat:userinforeturn.wechat,
                                gender:userinforeturn.gender
                            };
                            app.globalData.islogin=true;
                            swan.showModal({
                                title:"登录成功"
                            });
                            swan.switchTab({
                                url: '/pages/person/person'
                            });
                        }else{
                            swan.showModal({
                                title:"验证码有错误！"
                            });
                        }
                    }else{
                        swan.showModal({
                            title:"服务器出错了"
                        });
                    }
                },
                fail: err => {
                    swan.showModal({
                        title: '错误信息',
                        content: JSON.stringify(err)
                    });
                    console.log(err);
                    this.debug();
                }
            });
        }
    },
    register(){
        swan.navigateTo({
            url: '/pages/register/register'
        });
    },
    sendcode(e){
        let phone=this.data.phone;
        if(phone==''){
            swan.showModal({
                title:'您还没有输入手机号'
            });
            return false;
        }
        swan.request({
            url: fakedata.urls.sendcode,
            header: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Developer-Server-Domain': fakedata.host
            },
            method: 'POST',
            dataType: 'JSON',
            responseType: 'text',
            data: {
                phone:this.data.phone
            },
            success: res => {
                swan.showModal({
                    title:'验证法已经发送到您的手机'
                });
                this.setData({
                    cansendcode:false
                });
            },
            fail: err => {
                swan.showModal({
                    title: '错误信息',
                    content: JSON.stringify(err)
                });
                console.log(err);
            }
        });
    },
    changestatus(){
        this.setData({
            cansendcode:true
        })
    }
});
